// Copyright 2008 Peter William Birch <birchb@genyis.org>
//
// This software may be used and distributed according to the terms
// of the Genyris License, in the file "LICENSE", incorporated herein by reference.
//
package org.genyris.string;

import java.io.StringWriter;
import java.io.Writer;

import org.genyris.core.Constants;
import org.genyris.core.Exp;
import org.genyris.core.StrinG;
import org.genyris.exception.GenyrisException;
import org.genyris.interp.Closure;
import org.genyris.interp.Environment;
import org.genyris.interp.Interpreter;
import org.genyris.io.writerstream.WriterStream;

public class StringFormatMethod extends AbstractStringMethod {

	public static String getStaticName() {return Constants.FORMAT;};
	
	public StringFormatMethod(Interpreter interp) {
        super(interp, getStaticName());
    }

    public Exp bindAndExecute(Closure proc, Exp[] arguments, Environment env)
			throws GenyrisException {
		Exp theString = env.getSelf();
		if(arguments.length == 0) {
			return theString;
		}
		Writer out = new StringWriter();
		WriterStream str = new WriterStream(out);
		str.format((StrinG)theString, 0, arguments, env);
		return new StrinG(out.toString());
    }
}
